﻿namespace VirtualTemplates
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Web.Hosting;

    /// <summary>
    /// A virtual directory that maps to resources in an assembly.
    /// </summary>
    internal class AssemblyVirtualDirectory : VirtualDirectory
    {
        private readonly IEnumerable<AssemblyVirtualDirectory> directories;

        private readonly IEnumerable<AssemblyVirtualFile> files;

        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyVirtualDirectory"/> class.
        /// </summary>
        /// <param name="directories">The virtual directories.</param>
        /// <param name="files">The virtual files.</param>
        /// <param name="virtualPath">The virtual path.</param>
        public AssemblyVirtualDirectory(IEnumerable<AssemblyVirtualDirectory> directories, IEnumerable<AssemblyVirtualFile> files, string virtualPath)
            : base(virtualPath)
        {
            Contract.Requires(directories != null);
            Contract.Requires(files != null);

            this.directories = directories;
            this.files = files;
        }

        /// <summary>
        /// Gets a list of the files and subdirectories contained in this virtual directory.
        /// </summary>
        /// <returns>An object implementing the <see cref="T:System.Collections.IEnumerable"/> interface containing <see cref="T:System.Web.Hosting.VirtualFile"/> and <see cref="T:System.Web.Hosting.VirtualDirectory"/> objects.</returns>
        public override IEnumerable Children
        {
            get { return this.Directories.Cast<object>().Concat(this.Files.Cast<object>()); }
        }

        /// <summary>
        /// Gets a list of all the subdirectories contained in this directory.
        /// </summary>
        /// <returns>An object implementing the <see cref="T:System.Collections.IEnumerable"/> interface containing <see cref="T:System.Web.Hosting.VirtualDirectory"/> objects.</returns>
        public override IEnumerable Directories
        {
            get { return this.directories; }
        }

        /// <summary>
        /// Gets a list of all files contained in this directory.
        /// </summary>
        /// <returns>An object implementing the <see cref="T:System.Collections.IEnumerable"/> interface containing <see cref="T:System.Web.Hosting.VirtualFile"/> objects.</returns>
        public override IEnumerable Files
        {
            get { return this.files; }
        }
    }
}
